<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-12-01 09:56:58
 * @LastEditors: Sam
 * @LastEditTime: 2022-03-10 15:55:57
 */
namespace app\api\controller;
use app\__MODULE__\model\{Product,Exchange,ScoreLog,User};
use think\facade\Db;

class Score extends Base {

	    /**
     * 商品列表
     * @return [type] [description]
     */
    public function goods_list(){
    	$data = request()->param();
    	$where = [];
    	if(isset($data['keyword']) && $data['keyword'] !=''){
    		$where[] = ['keyword|store_name','like',"%{$data['keyword']}%"];
    	}
    	$page  = request()->param('page/d',1);
 		$limit = request()->param('limit/d',10);
    	$list  = Product::where('is_show',1)->field('id as goods_id,thumb,slide,out_price,score,stock')->where($where)->order('sort desc')->page($page,$limit)->select();
    	json_response(1,'success',$list);
    }

    /**
     * 商品详情
     * @return [type] [description]
     */
    /**
     * 商品详情
     * @return [type] [description]
     */
    public function detail(){
    	$goods_id = param_check('goods_id');
    	$goods_info   = Product::where('is_show',1)->where('stock','>',0)->where('id',$goods_id)->field('id as goods_id,thumb,slide,out_price,score,stock,content,stock')->find();
    	$goods_info->content = html_entity_decode($goods_info->content);
    	if(!$goods_info) json_response(0,'商品已下架或不存在');
    	json_response(1,'success',$goods_info);
    }

        /**
     * 商品兑换
     * @return void
     * @author Sam
     * date 2021-03-04
     */
    public function exchange(){
        $uid      = $this->get_user_id(0);
        $goods_id =  param_check('goods_id');
        $number   = param_check('number');
        $user      = User::get($uid);
        if(!$goods = Product::where('stock','>',0)->where('id',$goods_id)->find()) json_response(0,'商品不存在或者库存不足');
        if(!$goods->is_show) json_response(0,'商品已下架');
        if($user->score  < $goods->score * $number) json_response(0,'积分不足');
        Db::startTrans();
        try {
            Exchange::create([
                'uid'  => $uid,
                'goods_id' => $goods->id,
                'score'    => $goods->score * $number,
                'number'   => $number,
                'status'   => 0//道具直接完成  实物进入审核
            ]);
            $goods->stock -=1;
            $goods->save();
            //记录日志
            Exchange::out_score($uid,$goods->score * $number,1);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            show(0,$e->getMessage());
        }
        json_response(1,'兑换成功');
    }

        /**
     * 兑换记录
     * @return void
     * @author Sam
     * date 2021-12-14
     */
    public function exchangeLog(){
        $uid      = $this->get_user_id();
        $page     = \request()->param('page/d',1);
        $limit     = \request()->param('limit/d',10);
        $list     = Exchange::with(['goods'])->where('uid',$uid)->field('id,goods_id,uid,create_time,score,status')->append(['status_text'])->order('id desc')->page($page,$limit)->select();
        \json_response(1,'success',$list);
    }
}
